#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<pthread.h>
#include<errno.h>
#include<stdio.h>

void *thread_worker1(void*args);
void *thread_worker2(void*args);

int main(int argc,char * argv[])
{

	int               shared_var=1000;
	pthread_t         tid;
	
	pthread_attr_t    thread_attr;

	if(pthread_attr_init(&thread_attr))
	{
		printf("pthread_attr_init() failure:%s\n",strerror(errno));
		return  -1;

	}
	if(pthread_attr_setstacksize(&thread_attr,120*1024))
	{
		printf("pthread_attr_setstacksize() failure:%s\n",strerror(errno));
		return  -1;
	}
	if(pthread_attr_setdetachstate(&thread_attr,PTHREAD_CREATE_DETACHED))
	{
		printf("pthread_attr_setdetachstate() failure:%s\n",strerror(errno));
		return  -1;
	}
	pthread_create(&tid,&thread_attr,thread_worker1,&shared_var);//线程创建（pthread_t类型的指针，用来返回线程ID。属性，函数，参数）
	printf("thread worker1 tid[%ld] create ok \n",tid);
	pthread_create(&tid,NULL,thread_worker2,&shared_var);
	printf("thread worker2 tid[%ld] create ok \n",tid);
	pthread_attr_destroy(&thread_attr);
	pthread_join(tid,NULL);


	while(1)
	{
		printf("control thread shared_var:%d\n",shared_var);
		sleep(10);
	}
}
void  *thread_worker1(void *args)
{
	int          *ptr=(int*)args;
	if(!args)
	
	{
	
		printf("()get invalid arguments\n");
		pthread_exit(NULL);
	}
	printf("tread worker1 [%ld] start running ...\n",pthread_self());
	while(1)
	{
	
		printf("+++: before shared_var+++:%d\n",*ptr);
		*ptr+=1;
		sleep(2);
		printf("+++: after sleep shared_var+++:%d\n",*ptr);
	
	}
	printf("thread worker1 exit...\n");
	return  NULL;

}
void  *thread_worker2(void *args)
{

	int      *ptr=(int*)args;

	if(!args)
	{
	
		printf("() get invalid arguments\n");
		pthread_exit(NULL);
	}
	printf("thread worker2 [%ld] start running...\n",pthread_self());
	while(1)
	{
	
		printf("---: before shared_var++:%d\n",*ptr);
		*ptr+=1;
		sleep(2);
		printf("---: after sleep shared_var:%d\n",*ptr);
	}
	printf("thread worker2 exit...\n");
	return   NULL;
		
}





